using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Forum
{
    public partial class member_edit : System.Web.UI.UserControl
    {
        private void RegisterScriptIMG()
        {
            string js = MemberController.jsImage(txtImageUrl.ClientID);
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "setvalue", js);
            string popup = ResolveUrl("~/desktops/forum/avatar.aspx");
            lnkImage.NavigateUrl = "javascript:popup2('" + popup + "', 500, 400);";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!MemberController.EditMemberInfoRight(Request.QueryString["ID"])) return;

                if (!Request.IsAuthenticated) return;
                string username = Request.QueryString["ID"];
                lblUserName.Text = username;

                MemberController db = new MemberController();
                MemberInfo mem = db.Load(username);
                if (mem != null)
                {
                    lblPosts.Text = mem.Posts.ToString();
                    txtSlogan.Text = mem.Slogan;
                    txtImageUrl.Text = mem.ImageUrl;
                    txtSign.Text = mem.Sign;

                    db.Update(mem);
                }
                else
                {
                    mem = new MemberInfo();
                    mem.UserName = username;
                    db.Insert(mem);
                }
                RegisterScriptIMG();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if(!MemberController.EditMemberInfoRight(Request.QueryString["ID"])) return;

                MemberInfo mem = new MemberInfo();
                mem.UserName = lblUserName.Text;
                mem.ImageUrl = txtImageUrl.Text;
                mem.Slogan = txtSlogan.Text;
                mem.Sign = txtSign.Text.Trim();

                MemberController db = new MemberController();
                db.Update(mem);

                Response.Redirect("/");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/");
        }
    }
}